package com.web.service.impl;

import com.dto.BaseDto;
import com.dto.MessageEnum;
import com.entity.Festival;
import com.page.PageInfo;
import com.web.dao.FestivalDao;
import com.web.service.FestivalService;
import com.web.service.PlatformactivityrecodServiceForFestivalService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Optional;

@Service
public class FestivalServiceImpl implements FestivalService {
    @Resource
    FestivalDao festivalDao;
    @Resource
    PlatformactivityrecodServiceForFestivalService platformactivityrecodServiceForFestivalService;
    //查询全部
    public BaseDto<Festival> getAll() throws Exception {
        List<Festival> festivals = festivalDao.getAll();
        MessageEnum messageEnum = festivals.size() > 0? MessageEnum.GETALLSTUDENTSUCCESS: MessageEnum.GETALLSTUDENTNODATA;
        BaseDto<Festival> dto = new BaseDto(messageEnum,festivals);

        return dto;
    }
    //增删改查基本功能
    public BaseDto<Festival> selectByPrimaryKey(int fid) throws Exception {
        Festival festival = festivalDao.selectByPrimaryKey(fid);
        MessageEnum messageEnum = Optional.ofNullable(festival).isPresent()? MessageEnum.GETALLSTUDENTSUCCESS:MessageEnum.GETALLSTUDENTNODATA;
        BaseDto<Festival> dto = new BaseDto(messageEnum,festival);

        return dto;
    }

    public BaseDto<Festival> selectByPrimaryKeyWithActivitytype(int fid) throws Exception {
        Festival festival = festivalDao.selectByPrimaryKeyWithActivitytype(fid);
        MessageEnum messageEnum = Optional.ofNullable(festival).isPresent()? MessageEnum.GETALLSTUDENTSUCCESS:MessageEnum.GETALLSTUDENTNODATA;
        BaseDto<Festival> dto = new BaseDto(messageEnum,festival);

        return dto;
    }

    public BaseDto<Festival> getFestivalsByPage(PageInfo pageInfo) throws Exception {
        List<Festival> festivals = festivalDao.getFestivalsByPage(pageInfo);
        MessageEnum messageEnum = festivals.size() > 0? MessageEnum.GETBYPAGESUCCESS: MessageEnum.GETBYPAGEFAIL;
        BaseDto<Festival> dto = new BaseDto(messageEnum,festivals);

        return dto;
    }

    public BaseDto<Festival> add(Festival festival) throws Exception {
        int result = festivalDao.add(festival);
        MessageEnum messageEnum = result > 0? MessageEnum.ADDFESTIVALSUCCESS: MessageEnum.ADDFESTIVALFAIL;
        BaseDto<Festival> dto = new BaseDto(messageEnum,result);

        return dto;
    }

    public BaseDto<Festival> update(int fid) throws Exception {
        Festival festival = festivalDao.selectByPrimaryKey(fid);
        int result = festivalDao.update(festival);
        MessageEnum messageEnum = result > 0? MessageEnum.UPDATESUCCESS: MessageEnum.UPDATEFAIL;
        BaseDto<Festival> dto = new BaseDto(messageEnum,result);

        return dto;
    }

    //
    public BaseDto<Festival> ExistFestival(Integer fid) throws Exception {
        Festival festival = festivalDao.selectByPrimaryKey(fid);
        if(festival==null){
            throw new Exception("节日不存在");
        }
        MessageEnum messageEnum = Optional.ofNullable(festival).isPresent()? MessageEnum.GETALLSTUDENTSUCCESS:MessageEnum.GETALLSTUDENTNODATA;
        BaseDto<Festival> dto = new BaseDto(messageEnum,festival);
        return dto;
    }
}
